<template>
  <!-- 配置区服系数 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" class="search-form-new">
      <el-form-item label="游戏" prop="gameId">
        <el-select v-model="queryParams.gameId" placeholder="请选择游戏" clearable >
          <el-option v-for="dict in gameListAll" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="setServerNum">新增</el-button>
      </el-form-item>
    </el-form>

    <div class="table_box">
      <el-table v-loading="loading" :data="xishuList" @selection-change="handleSelectionChange">
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="游戏" align="center" prop="gameName">
          <!-- <template slot-scope="scope">
            <dict-tag :options="dict.type.game_type" :value="scope.row.type" />
          </template> -->
        </el-table-column>
        <el-table-column label="区服" align="center" prop="server" />
        <el-table-column label="系数" align="center" prop="num" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getListOn" />
    </div>

    <!-- 添加或修改公式配置对话框 -->
    <!-- 系数 -->
    <el-dialog :visible.sync="serverOpen" title="配置系数" width="400px" append-to-body>
      <el-form ref="formServe" :model="formServe" :rules="rules" label-width="80px">
        <el-form-item label="游戏" prop="type">
          <el-select v-model="formServe.type" @change="selectType" placeholder="请选择游戏" :disabled="form.id != null">
            <el-option v-for="dict in gameListAll" :key="dict.id" :label="dict.name" :value="dict.name"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="系统" prop="systemId" v-if="form.id == null && typed == 0">
          <el-select v-model="formServe.systemId" filterable placeholder="请选择">
            <el-option v-for="item in systemList" :key="item.id" :label="item.fieldName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="运营商" prop="carrierId" v-if="form.id == null && typed == 0">
          <el-select @change="getAreaList" v-model="formServe.carrierId" filterable placeholder="请选择">
            <el-option v-for="item in carrierList" :key="item.id" :label="item.fieldName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="大区" prop="groupId" v-if="(form.id == null && typed == 1) || typed == 0">
          <el-select v-model="formServe.groupId" filterable placeholder="请选择" @change="changefn">
            <el-option v-for="item in groupList" :key="item.id" :label="item.fieldName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="服务器" prop="serverId" v-if="(form.id == null && typed == 1) || typed == 0">
          <el-select v-model="formServe.serverId" filterable placeholder="请选择" @change="chgangeServee">
            <el-option v-for="item in serverList" :key="item.id" :label="item.fieldName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="系数" prop="num">
          <el-input v-model="formServe.num" placeholder="请输入系数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="xishuTrues">确 定</el-button>
        <el-button @click="serverOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listEquation,
  getEquation,
  delEquation,
  addEquation,
  updateEquation,
  exportEquation,
  getAppEquationList,
  addCoeff,
  getXISHUList,
  deleteXISHUList,
} from "@/api/equation/equation";
import gameDictionaryApi from "@/api/gameConfig/gameDictionary";

import gameListApi from "@/api/gameConfig/gameList";

export default {
  name: "Equation",
  data() {
    return {
      youxiId: null,
      youxiname: "",
      youxishuname: "",
      serverOpen: false,
      typed: "",
      copyVlaue: "",
      selectEquas: [],
      radio: 1,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公式配置表格数据
      equationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
      },
      // 表单参数
      form: {},
      formServe: {
        type: null,
      },
      // 表单校验
      rules: {},
      systemList: [], //系统
      carrierList: [], //运营商
      groupList: [], //大区
      serverList: [], //服务器
      gameListAll: [],
      xishuList: [], //系数list
    };
  },
  created() {
    // this.getList();
    this.gamelistAlls();
    this.getListOn();
  },
  methods: {
    async getListOn() {
      this.loading = true;
      const res = await getXISHUList(this.queryParams);
      //console.log(res);
      this.xishuList = res.rows;
      this.loading = false;
    },
    async gamelistAlls() {
      const res = await gameListApi.getGameList();
      //console.log(res);
      this.gameListAll = res.rows;
    },
    setServerNum() {
      this.serverOpen = true;
      this.formServe = {};
    },
    async xishuTrues() {
      let obj = {
        num: this.formServe.num,
        server: this.youxishuname,
        gameId: this.form.gameId,
        gameName: this.youxiname,
      };
      const res = await addCoeff(obj);
      //console.log(res);
      this.serverOpen = false;
      this.getListOn();
    },
    //获取系统
    // getSystem(){

    // },
    //调用未知接口
    //  async getAppEcution(){
    //     const res = await getAppEquationList()
    //     //console.log(res);
    //   },
    //点击看属性
    changedfn(item) {
      //console.log(item);
      this.form.formul = this.form.formul + item.fieldName;
    },
    copyAttr() {},
    selectType(e) {
      // this.sister = e
      //    this.formServe = null
      //   (this.formServe.systemId = null),
      //     (this.formServe.carrierId = null),
      //     (this.formServe.groupId = null),
      //     (this.formServe.serverId = null),
      // (this.form.systemId = null),
      // (this.form.carrierId = null),
      // (this.form.groupId = null),
      // (this.form.serverId = null),
      //     //console.log(e);
      //   let params = {};
      //   params.gameName = this.form.type;
      //   getAppEquationList(params).then((res) => {
      //     this.selectEquas = res.data;
      //   });
      this.formServe = {
        type: this.formServe.type,
        systemId: null,
        carrierId: null,
        groupId: null,
        serverId: null,
      };
      this.getSystem(e);
    },
    async getSystem(e) {
      const dated = this.gameListAll.filter((item) => item.name == e);
      //console.log(dated);
      this.typed = dated[0].type;
      this.form.gameId = dated[0].id;
      this.youxiname = dated[0].name;
      // this.youxiId = dated[0].name
      // 系统
      const res = await gameDictionaryApi.getChildList({
        gameId: dated[0].id,
        systemKey: "system",
      });
      //console.log(res);
      this.systemList = res.rows;
      //运营商
      const ress = await gameDictionaryApi.getChildList({
        gameId: dated[0].id,
        systemKey: "carrier",
      });
      //console.log(ress);
      this.carrierList = ress.rows;
      // 大区
      const resss = await gameDictionaryApi.getChildList({
        gameId: dated[0].id,
        systemKey: "group",
      });
      //console.log(resss);
      this.groupList = resss.rows;
    },
    async changefn(e) {
      //console.log(e);
      const resss = await gameDictionaryApi.getList({ parentId: e });
      //console.log(resss);
      this.serverList = resss.rows;
    },
    // 运营商改变  获取大区列表
    getAreaList(e, flag, areaId) {
      let obj = {
        gameId: this.form.gameId,
        systemKey: "group",
      };
      if (!flag) {
        // 不是回显的时候，再清除后面的选中
        this.form.groupName = "";
        this.form.groupId = "";
        this.form.serverName = "";
        this.form.serverId = "";
      } else {
        // 回显时，请求服务器列表
        this.getServiceList(areaId, true);
      }

      // 运营商改变 找出选择的运营商的对象
      if (e) {
        this.carrierList.forEach((i) => {
          if (i.id == e) {
            obj.systemKey = i.systemKey + "group";
            this.form.carrierName = i.fieldName;
          }
        });
      } else {
        this.form.carrierName = "";
      }
      gameDictionaryApi.getChildList(obj).then((res) => {
        this.groupList = res.rows;
        if (this.form.groupId) {
          this.groupList.forEach((r) => {
            if (r.id == this.form.groupId) {
              this.form.groupName = r.fieldName;
            }
          });
        }
      });
    },
    chgangeServe(e) {
      //console.log(e);
      let obj = this.serverList.find((item) => item.id === e);
      //console.log("111111", obj);
      this.form.formul = this.form.formul + obj.fieldName;
    },
    chgangeServee(e) {
      //console.log(e);
      let obj = this.serverList.find((item) => item.id === e);
      //console.log("111111", obj);
      this.youxishuname = obj.fieldName;
    },
    /** 查询公式配置列表 */
    getList() {
      this.loading = true;
      listEquation(this.queryParams).then((response) => {
        this.equationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sort: null,
        createBy: null,
        createTime: null,
        remark: null,
        formul: "",
        type: null,
        systemId: null,
        carrierId: null,
        groupId: null,
        serverId: null,
        gameId: null,
        num: null,
      };

      this.formServe = {
        type: null,
        systemId: null,
        carrierId: null,
        groupId: null,
        serverId: null,
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    async handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListOn();
    },
    getSousuoID(e) {
      //console.log(e);
      const dated = this.gameListAll.filter((item) => item.id == e);
      //console.log(dated);
      this.typed = dated[0].type;
      this.form.gameId = dated[0].id;
      this.youxiname = dated[0].name;
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      // this.handleQuery();
      this.queryParams.gameId = "";
      this.queryParams.pageNum = 1;
      this.getListOn();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公式配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEquation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公式配置";
        getAppEquationList({ gameName: this.form.type }).then((res) => {
          this.selectEquas = res.data;
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEquation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal
        .confirm('是否确认删除公式配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return deleteXISHUList(ids);
        })
        .then(() => {
          this.getListOn();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

  },
};
</script>
  